package com.example.shopping.goods;

import android.content.Intent;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.shopping.utils.BaseFragment;
import com.example.shopping.GoodsInfoActivity;
import com.example.shopping.R;
import com.example.shopping.utils.URLUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;


public class GoodsTypeFragment extends BaseFragment {
    ListView lv;
    List<GoodsTypeBean.DataBean> data;
    private GoodsAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_goods_type, container, false);
        lv = view.findViewById(R.id.frag_goods_lv_right);
        Bundle bundle = getArguments();
        int cid = bundle.getInt("cid",1);
        String url = URLUtils.selCategoryById+cid;
        data = new ArrayList<>();
        adapter = new GoodsAdapter(getContext(),data);
        lv.setAdapter(adapter);
        connectNetwork(url);
        setItemClick();
        return view;
    }

    private void setItemClick() {
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                GoodsTypeBean.DataBean bean = data.get(position);
                Intent intent=new Intent(getActivity(), GoodsInfoActivity.class);
                intent.putExtra("info",bean);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onSuccess(String result) {
        GoodsTypeBean bean = new Gson().fromJson(result,GoodsTypeBean.class);
        List<GoodsTypeBean.DataBean> list = bean.getData();
        data.addAll(list);
        adapter.notifyDataSetChanged();
    }
}

